home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / gnu / nethack.lha / nethack-3.1 / src / makemon.c < prev    next >
C/C++ Source or Header  |  1993-01-22  |  41KB  |  1,532 lines

  1. /*    SCCS Id: @(#)makemon.c    3.1    92/11/01    */
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #include "hack.h"
  6. #include "epri.h"
  7. #include "emin.h"
  8. #ifdef REINCARNATION
  9. # include <ctype.h>
  10. #endif
  11.  
  12. STATIC_VAR struct monst NEARDATA zeromonst;
  13.  
  14. #define uncommon(ptr) \
  15.     (((ptr)->geno & (G_GENOD | G_EXTINCT | G_NOGEN | G_UNIQ)) || \
  16.      (!Inhell ? ((ptr)->geno & G_HELL) : ((ptr)->maligntyp > A_NEUTRAL)))
  17.  
  18. #ifdef OVL0
  19. static boolean NDECL(cmavail);
  20. static int FDECL(align_shift, (struct permonst *));
  21. #endif /* OVL0 */
  22. STATIC_DCL boolean FDECL(is_home_elemental,(struct permonst *));
  23. STATIC_DCL boolean FDECL(wrong_elem_type, (struct permonst *));
  24. STATIC_DCL void FDECL(m_initgrp,(struct monst *,int,int,int));
  25. STATIC_DCL void FDECL(m_initthrow,(struct monst *,int,int));
  26. STATIC_DCL void FDECL(m_initweap,(struct monst *));
  27. #ifdef OVL1
  28. static void FDECL(m_initinv,(struct monst *));
  29. #endif /* OVL1 */
  30.  
  31. extern int monstr[];
  32.  
  33. #define m_initsgrp(mtmp, x, y)    m_initgrp(mtmp, x, y, 3)
  34. #define m_initlgrp(mtmp, x, y)    m_initgrp(mtmp, x, y, 10)
  35. #define toostrong(monindx, lev) (monstr[monindx] > lev)
  36. #define tooweak(monindx, lev)    (monstr[monindx] < lev)
  37.  
  38. #ifdef OVLB
  39. STATIC_OVL boolean
  40. is_home_elemental(ptr)
  41. register struct permonst *ptr;
  42. {
  43.     if (ptr->mlet != S_ELEMENTAL) return FALSE;
  44.     if (!In_endgame(&u.uz)) return FALSE;
  45.     switch(monsndx(ptr)) {
  46.         case PM_AIR_ELEMENTAL: return Is_airlevel(&u.uz);
  47.         case PM_FIRE_ELEMENTAL: return Is_firelevel(&u.uz);
  48.         case PM_EARTH_ELEMENTAL: return Is_earthlevel(&u.uz);
  49.         case PM_WATER_ELEMENTAL: return Is_waterlevel(&u.uz);
  50.     }
  51.     return FALSE;    /* shouldn't be reached */
  52. }
  53.  
  54. /*
  55.  * Return true if the given monster cannot exist on this elemental level.
  56.  */
  57. STATIC_OVL boolean
  58. wrong_elem_type(ptr)
  59.     register struct permonst *ptr;
  60. {
  61.     if (Is_earthlevel(&u.uz)) {
  62.     /* no restrictions? */
  63.     } else if (Is_waterlevel(&u.uz)) {
  64.     /* just monsters that can swim */
  65.     if(!is_swimmer(ptr)) return TRUE;
  66.     } else if (Is_firelevel(&u.uz)) {
  67.     if(!resists_fire(ptr)) return TRUE;
  68.     } else if (Is_airlevel(&u.uz)) {
  69.     if(!(is_flyer(ptr) && ptr->mlet != S_TRAPPER) && !is_floater(ptr)
  70.        && !amorphous(ptr) && !noncorporeal(ptr) && !is_whirly(ptr))
  71.         return TRUE;
  72.     }
  73.     return FALSE;
  74. }
  75.  
  76. STATIC_OVL void
  77. m_initgrp(mtmp, x, y, n)    /* make a group just like mtmp */
  78. register struct monst *mtmp;
  79. register int x, y, n;
  80. {
  81.     coord mm;
  82.     register int cnt = rnd(n);
  83.     struct monst *mon;
  84.  
  85. /*
  86.  *    Temporary kludge to cut down on swarming at lower character levels
  87.  *    till we can get this game a little more balanced. [mrs]
  88.  */
  89.     cnt /= (u.ulevel < 3) ? 4 : (u.ulevel < 5) ? 2 : 1;
  90.     if(!cnt) cnt++;
  91.  
  92.     mm.x = x;
  93.     mm.y = y;
  94.     while(cnt--) {
  95.         if (peace_minded(mtmp->data)) continue;
  96.         /* Don't create groups of peaceful monsters since they'll get
  97.          * in our way.  If the monster has a percentage chance so some
  98.          * are peaceful and some are not, the result will just be a
  99.          * smaller group.
  100.          */
  101.         if (enexto(&mm, mm.x, mm.y, mtmp->data)) {
  102.             mon = makemon(mtmp->data, mm.x, mm.y);
  103.             mon->mpeaceful = FALSE;
  104.             set_malign(mon);
  105.             /* Undo the second peace_minded() check in makemon(); if the
  106.              * monster turned out to be peaceful the first time we
  107.              * didn't create it at all; we don't want a second check.
  108.              */
  109.         }
  110.     }
  111. }
  112.  
  113. STATIC_OVL
  114. void
  115. m_initthrow(mtmp,otyp,oquan)
  116. struct monst *mtmp;
  117. int otyp,oquan;
  118. {
  119.     register struct obj *otmp;
  120.  
  121.     otmp = mksobj(otyp, TRUE, FALSE);
  122.     otmp->quan = (long) rn1(oquan, 3);
  123.     otmp->owt = weight(otmp);
  124.     if (otyp == ORCISH_ARROW) otmp->opoisoned = TRUE;
  125.     mpickobj(mtmp, otmp);
  126. }
  127.  
  128. #endif /* OVLB */
  129. #ifdef OVL2
  130.  
  131. STATIC_OVL void
  132. m_initweap(mtmp)
  133. register struct monst *mtmp;
  134. {
  135.     register struct permonst *ptr = mtmp->data;
  136.     register int mm = monsndx(ptr);
  137. #ifdef REINCARNATION
  138.     if (Is_rogue_level(&u.uz)) return;
  139. #endif
  140. /*
  141.  *    first a few special cases:
  142.  *
  143.  *        giants get a boulder to throw sometimes.
  144.  *        ettins get clubs
  145.  *        kobolds get darts to throw
  146.  *        centaurs get some sort of bow & arrows or bolts
  147.  *        soldiers get all sorts of things.
  148.  *        kops get clubs & cream pies.
  149.  */
  150.     switch (mtmp->data->mlet) {
  151.         case S_GIANT:
  152.         if (rn2(2)) (void)mongets(mtmp, (ptr != &mons[PM_ETTIN]) ?
  153.                     BOULDER : CLUB);
  154.         break;
  155.         case S_HUMAN:
  156.         if(is_mercenary(ptr)) {
  157.             int w1 = 0, w2 = 0;
  158.             switch (mm) {
  159.  
  160.             case PM_WATCHMAN:
  161. #ifdef ARMY
  162.             case PM_SOLDIER:
  163. #endif
  164.               if (!rn2(3)) {
  165.                   w1 = rn1(BEC_DE_CORBIN - PARTISAN + 1, PARTISAN);
  166.                   w2 = rn2(2) ? DAGGER : KNIFE;
  167.               } else w1 = rn2(2) ? SPEAR : SHORT_SWORD;
  168.               break;
  169. #ifdef ARMY
  170.             case PM_SERGEANT:
  171.               w1 = rn2(2) ? FLAIL : MACE;
  172.               break;
  173.             case PM_LIEUTENANT:
  174.               w1 = rn2(2) ? BROADSWORD : LONG_SWORD;
  175.               break;
  176.             case PM_CAPTAIN:
  177. #endif
  178.             case PM_WATCH_CAPTAIN:
  179.               w1 = rn2(2) ? LONG_SWORD : SILVER_SABER;
  180.               break;
  181.             default:
  182.               if (!rn2(4)) w1 = DAGGER;
  183.               if (!rn2(7)) w2 = SPEAR;
  184.               break;
  185.             }
  186.             if (w1) (void)mongets(mtmp, w1);
  187.             if (!w2 && w1 != DAGGER && !rn2(4)) w2 = KNIFE;
  188.             if (w2) (void)mongets(mtmp, w2);
  189.         } else if (is_elf(ptr)) {
  190.             if (rn2(2))
  191.             (void) mongets(mtmp,
  192.                    rn2(2) ? ELVEN_MITHRIL_COAT : ELVEN_CLOAK);
  193.             if (rn2(2)) (void)mongets(mtmp, ELVEN_LEATHER_HELM);
  194.             else if (!rn2(4)) (void)mongets(mtmp, ELVEN_BOOTS);
  195.             if (rn2(2)) (void)mongets(mtmp, ELVEN_DAGGER);
  196.             switch (rn2(3)) {
  197.             case 0:
  198.                 if (!rn2(4)) (void)mongets(mtmp, ELVEN_SHIELD);
  199.                 if (rn2(3)) (void)mongets(mtmp, ELVEN_SHORT_SWORD);
  200.                 (void)mongets(mtmp, ELVEN_BOW);
  201.                 m_initthrow(mtmp, ELVEN_ARROW, 12);
  202.                 break;
  203.             case 1:
  204.                 (void)mongets(mtmp, ELVEN_BROADSWORD);
  205.                 if (rn2(2)) (void)mongets(mtmp, ELVEN_SHIELD);
  206.                 break;
  207.             case 2:
  208.                 if (rn2(2)) {
  209.                 (void)mongets(mtmp, ELVEN_SPEAR);
  210.                 (void)mongets(mtmp, ELVEN_SHIELD);
  211.                 }
  212.                 break;
  213.             }
  214.             if(mtmp->data == &mons[PM_ELVENKING])
  215.             (void)mongets(mtmp, PICK_AXE);
  216.         }
  217.         break;
  218.  
  219.         case S_ANGEL:
  220.         {
  221.             int spe2;
  222.             /* create minion stuff; can't use mongets */
  223.             struct obj *otmp = mksobj(LONG_SWORD, FALSE, FALSE);
  224.  
  225.             /* maybe make it special */
  226.             if(!rn2(20) || is_lord(mtmp->data))
  227.             otmp = oname(otmp, artiname(
  228.                 rn2(2) ? ART_DEMONBANE : ART_SUNSWORD), 0);
  229.             bless(otmp);
  230.             otmp->oerodeproof = TRUE;
  231.             spe2 = rn2(4);
  232.             otmp->spe = max(otmp->spe, spe2);
  233.             mpickobj(mtmp, otmp);
  234.  
  235.             otmp = mksobj(!rn2(4) || is_lord(mtmp->data) ?
  236.                   SHIELD_OF_REFLECTION : LARGE_SHIELD,
  237.                   FALSE, FALSE);
  238.             otmp->cursed = FALSE;
  239.             otmp->oerodeproof = TRUE;
  240.             otmp->spe = 0;
  241.             mpickobj(mtmp, otmp);
  242.         }
  243.         break;
  244.  
  245.         case S_HUMANOID:
  246.         if (mm == PM_HOBBIT) {
  247.             switch (rn2(3)) {
  248.             case 0:
  249.                 (void)mongets(mtmp, DAGGER);
  250.                 break;
  251.             case 1:
  252.                 (void)mongets(mtmp, ELVEN_DAGGER);
  253.                 break;
  254.             case 2:
  255.                 (void)mongets(mtmp, SLING);
  256.                 break;
  257.               }
  258.             if (!rn2(10)) (void)mongets(mtmp, ELVEN_MITHRIL_COAT);
  259.             if (!rn2(10)) (void)mongets(mtmp, DWARVISH_CLOAK);
  260.         } else if (is_dwarf(ptr)) {
  261.             if (rn2(7)) (void)mongets(mtmp, DWARVISH_CLOAK);
  262.             if (rn2(7)) (void)mongets(mtmp, IRON_SHOES);
  263.             if (!rn2(4)) {
  264.             (void)mongets(mtmp, DWARVISH_SHORT_SWORD);
  265.             /* note: you can't use a mattock with a shield */
  266.             if (rn2(2)) (void)mongets(mtmp, DWARVISH_MATTOCK);
  267.             else {
  268.                 (void)mongets(mtmp, AXE);
  269.                 (void)mongets(mtmp, DWARVISH_ROUNDSHIELD);
  270.             }
  271.             (void)mongets(mtmp, DWARVISH_IRON_HELM);
  272.             if (!rn2(3))
  273.                 (void)mongets(mtmp, DWARVISH_MITHRIL_COAT);
  274.             } else {
  275.             (void)mongets(mtmp, !rn2(3) ? PICK_AXE : DAGGER);
  276.             }
  277.         }
  278.         break;
  279. # ifdef KOPS
  280.         case S_KOP:        /* create Keystone Kops with cream pies to
  281.                  * throw. As suggested by KAA.       [MRS]
  282.                  */
  283.         if (!rn2(4)) m_initthrow(mtmp, CREAM_PIE, 2);
  284.         if (!rn2(3)) (void)mongets(mtmp,(rn2(2)) ? CLUB : RUBBER_HOSE);
  285.         break;
  286. # endif
  287.         case S_ORC:
  288.         if(rn2(2)) (void)mongets(mtmp, ORCISH_HELM);
  289.         switch (mm != PM_ORC_CAPTAIN ? mm :
  290.             rn2(2) ? PM_MORDOR_ORC : PM_URUK_HAI) {
  291.             case PM_MORDOR_ORC:
  292.             if(!rn2(3)) (void)mongets(mtmp, SCIMITAR);
  293.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHIELD);
  294.             if(!rn2(3)) (void)mongets(mtmp, KNIFE);
  295.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_CHAIN_MAIL);
  296.             break;
  297.             case PM_URUK_HAI:
  298.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_CLOAK);
  299.             if(!rn2(3)) (void)mongets(mtmp, ORCISH_SHORT_SWORD);
  300.             if(!rn2(3)) (void)mongets(mtmp, IRON_SHOES);
  301.             if(!rn2(3)) {
  302.                 (void)mongets(mtmp, ORCISH_BOW);
  303.                 m_initthrow(mtmp, ORCISH_ARROW, 12);
  304.             }
  305.             if(!rn2(3)) (void)mongets(mtmp, URUK_HAI_SHIELD);
  306.             break;
  307.             default:
  308.             if (mm != PM_ORC_SHAMAN && rn2(2))
  309.               (void)mongets(mtmp, (mm == PM_GOBLIN || rn2(2) == 0)
  310.                            ? ORCISH_DAGGER : SCIMITAR);
  311.         }
  312.         break;
  313.         case S_OGRE:
  314.         if (!rn2(mm == PM_OGRE_KING ? 3 : mm == PM_OGRE_LORD ? 6 : 12))
  315.             (void) mongets(mtmp, BATTLE_AXE);
  316.         break;
  317.         case S_KOBOLD:
  318.         if (!rn2(4)) m_initthrow(mtmp, DART, 12);
  319.         break;
  320.  
  321.         case S_CENTAUR:
  322.         if (rn2(2)) {
  323.             if(ptr == &mons[PM_FOREST_CENTAUR]) {
  324.             (void)mongets(mtmp, BOW);
  325.             m_initthrow(mtmp, ARROW, 12);
  326.             } else {
  327.             (void)mongets(mtmp, CROSSBOW);
  328.             m_initthrow(mtmp, CROSSBOW_BOLT, 12);
  329.             }
  330.         }
  331.         break;
  332.         case S_WRAITH:
  333.         (void)mongets(mtmp, KNIFE);
  334.         (void)mongets(mtmp, LONG_SWORD);
  335.         break;
  336.         case S_DEMON:
  337.         switch (mm) {
  338.             case PM_BALROG:
  339.             (void)mongets(mtmp, BULLWHIP);
  340.             (void)mongets(mtmp, BROADSWORD);
  341.             break;
  342.             case PM_ORCUS:
  343.             (void)mongets(mtmp, WAN_DEATH); /* the Wand of Orcus */
  344.             break;
  345.             case PM_HORNED_DEVIL:
  346.             (void)mongets(mtmp, rn2(4) ? TRIDENT : BULLWHIP);
  347.             break;
  348.             case PM_ICE_DEVIL:
  349.             if (!rn2(4)) (void)mongets(mtmp, SPEAR);
  350.             break;
  351.             case PM_ASMODEUS:
  352.             (void)mongets(mtmp, WAN_COLD);
  353.             break;
  354.             case PM_DISPATER:
  355.             (void)mongets(mtmp, WAN_STRIKING);
  356.             break;
  357.             case PM_YEENOGHU:
  358.             (void)mongets(mtmp, FLAIL);
  359.             break;
  360.         }
  361.         /* prevent djinnis and mail daemons from leaving objects when
  362.          * they vanish
  363.          */
  364.         if (!is_demon(ptr)) break;
  365.         /* fall thru */
  366. /*
  367.  *    Now the general case, ~40% chance of getting some type
  368.  *    of weapon. TODO: Add more weapons types (use bigmonst());
  369.  */
  370.         default:
  371.         switch(rnd(12)) {
  372.             case 1:
  373.             m_initthrow(mtmp, DART, 12);
  374.             break;
  375.             case 2:
  376.             (void) mongets(mtmp, CROSSBOW);
  377.             m_initthrow(mtmp, CROSSBOW_BOLT, 12);
  378.             break;
  379.             case 3:
  380.             (void) mongets(mtmp, BOW);
  381.             m_initthrow(mtmp, ARROW, 12);
  382.             break;
  383.             case 4:
  384.             m_initthrow(mtmp, DAGGER, 3);
  385.             break;
  386.             case 5:
  387.             (void) mongets(mtmp, AKLYS);
  388.             break;
  389.             default:
  390.             break;
  391.         }
  392.         break;
  393.     }
  394. #ifdef MUSE
  395.     if ((int) mtmp->m_lev > rn2(70))
  396.         (void) mongets(mtmp, rnd_offensive_item(mtmp));
  397. #endif
  398. }
  399.  
  400. #endif /* OVL2 */
  401. #ifdef OVL1
  402.  
  403. static void
  404. m_initinv(mtmp)
  405. register struct    monst    *mtmp;
  406. {
  407.     register int cnt;
  408.     register struct obj *otmp;
  409.     register struct permonst *ptr = mtmp->data;
  410. #ifdef REINCARNATION
  411.     if (Is_rogue_level(&u.uz)) return;
  412. #endif
  413. /*
  414.  *    Soldiers get armour & rations - armour approximates their ac.
  415.  *    Nymphs may get mirror or potion of object detection.
  416.  */
  417.     switch(ptr->mlet) {
  418.  
  419.         case S_HUMAN:
  420.         if(is_mercenary(ptr)) {
  421.             register int mac;
  422. #ifdef MUSE
  423.             switch(monsndx(ptr)) {
  424.             case PM_GUARD: mac = -1; break;
  425. # ifdef ARMY
  426.             case PM_SOLDIER: mac = 3; break;
  427.             case PM_SERGEANT: mac = 0; break;
  428.             case PM_LIEUTENANT: mac = -2; break;
  429.             case PM_CAPTAIN: mac = -3; break;
  430. # endif
  431.             case PM_WATCHMAN: mac = 3; break;
  432.             case PM_WATCH_CAPTAIN: mac = -2; break;
  433.             default: impossible("odd mercenary %d?", monsndx(ptr));
  434.                 mac = 0;
  435.                 break;
  436.             }
  437. #else
  438.             mac = ptr->ac;
  439. #endif
  440.  
  441.             if (mac < -1 && rn2(5))
  442.             mac += 7 + mongets(mtmp, (rn2(5)) ?
  443.                        PLATE_MAIL : CRYSTAL_PLATE_MAIL);
  444.             else if (mac < 3 && rn2(5))
  445.             mac += 6 + mongets(mtmp, (rn2(3)) ?
  446.                        SPLINT_MAIL : BANDED_MAIL);
  447.             else if (rn2(5))
  448.             mac += 3 + mongets(mtmp, (rn2(3)) ?
  449.                        RING_MAIL : STUDDED_LEATHER_ARMOR);
  450.             else
  451.             mac += 2 + mongets(mtmp, LEATHER_ARMOR);
  452.  
  453.             if (mac < 10 && rn2(3))
  454.             mac += 1 + mongets(mtmp, HELMET);
  455.             else if (mac < 10 && rn2(2))
  456.             mac += 1 + mongets(mtmp, DENTED_POT);
  457.             if (mac < 10 && rn2(3))
  458.             mac += 1 + mongets(mtmp, SMALL_SHIELD);
  459.             else if (mac < 10 && rn2(2))
  460.             mac += 2 + mongets(mtmp, LARGE_SHIELD);
  461.             if (mac < 10 && rn2(3))
  462.             mac += 1 + mongets(mtmp, LOW_BOOTS);
  463.             else if (mac < 10 && rn2(2))
  464.             mac += 2 + mongets(mtmp, HIGH_BOOTS);
  465.             if (mac < 10 && rn2(3))
  466.             mac += 1 + mongets(mtmp, LEATHER_GLOVES);
  467.             else if (mac < 10 && rn2(2))
  468.             mac += 1 + mongets(mtmp, ELVEN_CLOAK);
  469.  
  470. #ifndef MUSE
  471.             if (mac != 10 && rn2(5)) {    /* make up the difference */
  472.             otmp = mksobj(RIN_PROTECTION, FALSE, FALSE);
  473.             otmp->spe = (10 - mac + rn2(3) - rn2(3));
  474.             if(otmp->spe < 0) curse(otmp);
  475.             mpickobj(mtmp, otmp);
  476.             }
  477. #endif
  478. #ifdef ARMY
  479.             if(ptr != &mons[PM_GUARD] &&
  480.             ptr != &mons[PM_WATCHMAN] &&
  481.             ptr != &mons[PM_WATCH_CAPTAIN]) {
  482.             if (!rn2(3)) (void) mongets(mtmp, K_RATION);
  483.             if (!rn2(2)) (void) mongets(mtmp, C_RATION);
  484. # ifdef MUSE
  485.             if (ptr != &mons[PM_SOLDIER] && !rn2(3))
  486.                 (void) mongets(mtmp, BUGLE);
  487. # endif
  488.             } else
  489. #endif
  490.                if (ptr == &mons[PM_WATCHMAN] && rn2(3))
  491.                 (void) mongets(mtmp, TIN_WHISTLE);
  492.         } else if (ptr == &mons[PM_SHOPKEEPER]) {
  493.             (void) mongets(mtmp,SKELETON_KEY);
  494.         }
  495.         break;
  496.  
  497.         case S_NYMPH:
  498.         if(!rn2(2)) (void) mongets(mtmp, MIRROR);
  499.         if(!rn2(2)) (void) mongets(mtmp, POT_OBJECT_DETECTION);
  500.         break;
  501.  
  502.         case S_GIANT:
  503.         if (ptr == &mons[PM_MINOTAUR])
  504.             (void) mongets(mtmp, WAN_DIGGING);
  505.         else if (is_giant(ptr)) {
  506.             for(cnt = rn2((int)(mtmp->m_lev / 2)); cnt; cnt--) {
  507.                 otmp = mksobj(rnd_class(DILITHIUM_CRYSTAL,LUCKSTONE-1),FALSE,FALSE);
  508.                 otmp->quan = (long) rn1(2, 3);
  509.                 otmp->owt = weight(otmp);
  510.                 mpickobj(mtmp, otmp);
  511.             }
  512.         }
  513.         break;
  514.         case S_WRAITH:
  515.         if (ptr == &mons[PM_NAZGUL]) {
  516.             otmp = mksobj(RIN_INVISIBILITY, FALSE, FALSE);
  517.             curse(otmp);
  518.             mpickobj(mtmp, otmp);
  519.         }
  520.         break;
  521.         case S_QUANTMECH:
  522.         if (!rn2(20)) {
  523.             struct obj *cat;
  524.  
  525.             otmp = mksobj(LARGE_BOX, FALSE, FALSE);
  526.     /* actually, whether this is a corpse or a live cat shouldn't
  527.        really be decided until the box is opened... */
  528.             cat = mksobj(CORPSE, FALSE, FALSE);
  529.             cat->corpsenm = PM_HOUSECAT;
  530.             cat->owt = weight(cat);
  531.             cat = oname(cat, "Schroedinger's Cat", FALSE);
  532.             cat->nobj = otmp->cobj;
  533.             otmp->cobj = cat;
  534.             otmp->owt = weight(otmp);
  535.             mpickobj(mtmp, otmp);
  536.         }
  537.         break;
  538.         case S_LEPRECHAUN:
  539.         mtmp->mgold = (long) d(level_difficulty(), 30);
  540.         break;
  541.         default:
  542.         break;
  543.     }
  544.  
  545. #ifdef ARMY    /* ordinary soldiers rarely have access to magic (or gold :-) */
  546.     if (ptr == &mons[PM_SOLDIER] && rn2(13)) return;
  547. #endif
  548. #ifdef MUSE
  549.     if ((int) mtmp->m_lev > rn2(30))
  550.         (void) mongets(mtmp, rnd_defensive_item(mtmp));
  551.     if ((int) mtmp->m_lev > rn2(100))
  552.         (void) mongets(mtmp, rnd_misc_item(mtmp));
  553. #endif
  554.     if (likes_gold(ptr) && !mtmp->mgold && !rn2(5))
  555.         mtmp->mgold =
  556.               (long) d(level_difficulty(), mtmp->minvent ? 5 : 10);
  557. }
  558.  
  559. /*
  560.  * called with [x,y] = coordinates;
  561.  *    [0,0] means anyplace
  562.  *    [u.ux,u.uy] means: near player (if !in_mklev)
  563.  *
  564.  *    In case we make a monster group, only return the one at [x,y].
  565.  */
  566. struct monst *
  567. makemon(ptr, x, y)
  568. register struct permonst *ptr;
  569. register int    x, y;
  570. {
  571.     register struct monst *mtmp;
  572.     register int    ct;
  573.     boolean anything = (!ptr);
  574.     boolean byyou = (x == u.ux && y == u.uy);
  575.  
  576.     /* if caller wants random location, do it here */
  577.     if(x == 0 && y == 0) {
  578.         int tryct = 0;    /* careful with bigrooms */
  579.         do {
  580.             x = rn1(COLNO-3,2);
  581.             y = rn2(ROWNO);
  582.         } while(!goodpos(x, y, (struct monst *)0, ptr) ||
  583.             (!in_mklev && tryct++ < 50 && cansee(x, y)));
  584.     } else if (byyou && !in_mklev) {
  585.         coord bypos;
  586.  
  587.         if(enexto(&bypos, u.ux, u.uy, ptr)) {
  588.             x = bypos.x;
  589.             y = bypos.y;
  590.         } else
  591.             return((struct monst *)0);
  592.     }
  593.  
  594.     /* if a monster already exists at the position, return */
  595.     if(MON_AT(x, y))
  596.         return((struct monst *) 0);
  597.  
  598.     if(ptr){
  599.         /* if you are to make a specific monster and it has
  600.            already been genocided, return */
  601.         if(ptr->geno & G_GENOD) return((struct monst *) 0);
  602.     } else {
  603.         /* make a random (common) monster that can survive here.
  604.          * (the special levels ask for random monsters at specific
  605.          * positions, causing mass drowning on the medusa level,
  606.          * for instance.)
  607.          */
  608.         int tryct = 0;    /* maybe there are no good choices */
  609.         do {
  610.             if(!(ptr = rndmonst())) {
  611. #ifdef DEBUG
  612.                 pline("Warning: no monster.");
  613. #endif
  614.                 return((struct monst *) 0);    /* no more monsters! */
  615.             }
  616.         } while(!goodpos(x, y, (struct monst *)0, ptr) && tryct++ < 50);
  617.     }
  618.     /* if it's unique, don't ever make it again */
  619.     if (ptr->geno & G_UNIQ) ptr->geno |= G_EXTINCT;
  620.  
  621.     mtmp = newmonst(ptr->pxlth);
  622.     *mtmp = zeromonst;        /* clear all entries in structure */
  623.     for(ct = 0; ct < ptr->pxlth; ct++)
  624.         ((char *) &(mtmp->mextra[0]))[ct] = 0;
  625.     mtmp->nmon = fmon;
  626.     fmon = mtmp;
  627.     mtmp->m_id = flags.ident++;
  628.     mtmp->data = ptr;
  629.     mtmp->mxlth = ptr->pxlth;
  630.  
  631.     mtmp->m_lev = adj_lev(ptr);
  632.     if (is_golem(ptr))
  633.         mtmp->mhpmax = mtmp->mhp = golemhp(monsndx(ptr));
  634.     else if (is_rider(ptr)) {
  635.         /* We want low HP, but a high mlevel so they can attack well */
  636.         mtmp->mhpmax = mtmp->mhp = d(10,8);
  637.     } else if(ptr->mlevel > 49) {
  638.         /* "special" fixed hp monster
  639.          * the hit points are encoded in the mlevel in a somewhat strange
  640.          * way to fit in the 50..127 positive range of a signed character
  641.          * above the 1..49 that indicate "normal" monster levels */
  642.         mtmp->mhpmax = mtmp->mhp = 2*(ptr->mlevel - 6);
  643.         mtmp->m_lev = mtmp->mhp / 4;    /* approximation */
  644.     } else if((ptr->mlet == S_DRAGON) && (ptr >= &mons[PM_GRAY_DRAGON]))
  645.         mtmp->mhpmax = mtmp->mhp = mtmp->m_lev*8;
  646.     else if(!mtmp->m_lev) mtmp->mhpmax = mtmp->mhp = rnd(4);
  647.     else if(is_home_elemental(ptr))
  648.         mtmp->mhpmax = mtmp->mhp = 3 * d((int)mtmp->m_lev, 8);
  649.     else mtmp->mhpmax = mtmp->mhp = d((int)mtmp->m_lev, 8);
  650.  
  651.     if (is_female(ptr)) mtmp->female = TRUE;
  652.     else if (is_male(ptr)) mtmp->female = FALSE;
  653.     else mtmp->female = rn2(2);    /* ignored for neuters */
  654.  
  655.     place_monster(mtmp, x, y);
  656.     mtmp->mcansee = mtmp->mcanmove = TRUE;
  657.     mtmp->mpeaceful = peace_minded(ptr);
  658.  
  659.     switch(ptr->mlet) {
  660.         case S_MIMIC:
  661.             set_mimic_sym(mtmp);
  662.             break;
  663.         case S_SPIDER:
  664.         case S_SNAKE:
  665.             if(in_mklev)
  666.                 if(x && y)
  667.                 (void) mkobj_at(0, x, y, TRUE);
  668.             if(hides_under(ptr) && OBJ_AT(x, y))
  669.                 mtmp->mundetected = TRUE;
  670.             break;
  671.         case S_STALKER:
  672.         case S_EEL:
  673.             mtmp->minvis = TRUE;
  674.             break;
  675.         case S_LEPRECHAUN:
  676.             mtmp->msleep = TRUE;
  677.             break;
  678.         case S_JABBERWOCK:
  679.         case S_NYMPH:
  680.             if(rn2(5) && !u.uhave.amulet) mtmp->msleep = TRUE;
  681.             break;
  682.         case S_ORC:
  683.             if(pl_character[0] == 'E') mtmp->mpeaceful = FALSE;
  684.             break;
  685.         case S_UNICORN:
  686.             if (sgn(u.ualign.type) == sgn(ptr->maligntyp))
  687.                 mtmp->mpeaceful = TRUE;
  688.             break;
  689.     }
  690.     if (ptr == &mons[PM_CHAMELEON]) {
  691.         /* If you're protected with a ring, don't create
  692.          * any shape-changing chameleons -dgk
  693.          */
  694.         if (Protection_from_shape_changers)
  695.             mtmp->cham = FALSE;
  696.         else {
  697.             mtmp->cham = TRUE;
  698.             (void) newcham(mtmp, rndmonst());
  699.         }
  700.     } else if (ptr == &mons[PM_WIZARD_OF_YENDOR]) {
  701.         mtmp->iswiz = TRUE;
  702.         flags.no_of_wizards++;
  703.     } else if (ptr == &mons[PM_VLAD_THE_IMPALER])
  704.         (void) mongets(mtmp, CANDELABRUM_OF_INVOCATION);
  705. #ifdef MULDGN
  706.     else if (ptr->msound == MS_NEMESIS)
  707.         (void) mongets(mtmp, BELL_OF_OPENING);
  708. #else
  709.     else if (ptr == &mons[PM_MEDUSA])
  710.         (void) mongets(mtmp, BELL_OF_OPENING);
  711. #endif
  712.  
  713.     if(in_mklev) {
  714.         if(((is_ndemon(ptr)) ||
  715.             (ptr == &mons[PM_WUMPUS]) ||
  716.             (ptr == &mons[PM_LONG_WORM]) ||
  717.             (ptr == &mons[PM_GIANT_EEL])) && !u.uhave.amulet && rn2(5))
  718.             mtmp->msleep = TRUE;
  719.     } else {
  720.         if(byyou) {
  721.             newsym(mtmp->mx,mtmp->my);
  722.             set_apparxy(mtmp);
  723.         }
  724.     }
  725.     if(is_dprince(ptr)) {
  726.         mtmp->mpeaceful = mtmp->minvis = TRUE;
  727.         if (uwep && uwep->oartifact == ART_EXCALIBUR)
  728.         mtmp->mpeaceful = mtmp->mtame = FALSE;
  729.     }
  730.     if ( (ptr == &mons[PM_LONG_WORM]) && (mtmp->wormno = get_wormno()) ) {
  731.         /* we can now create worms with tails - 11/91 */
  732.         initworm(mtmp, rn2(5));
  733.         if (count_wsegs(mtmp)) place_worm_tail_randomly(mtmp, x, y);
  734.     }
  735.     set_malign(mtmp);        /* having finished peaceful changes */
  736.     if(anything) {
  737.         if((ptr->geno & G_SGROUP) && rn2(2))
  738.         m_initsgrp(mtmp, mtmp->mx, mtmp->my);
  739.         else if(ptr->geno & G_LGROUP) {
  740.             if(rn2(3))  m_initlgrp(mtmp, mtmp->mx, mtmp->my);
  741.             else        m_initsgrp(mtmp, mtmp->mx, mtmp->my);
  742.         }
  743.     }
  744.  
  745.     if(is_armed(ptr))
  746.         m_initweap(mtmp);    /* equip with weapons / armor */
  747.     m_initinv(mtmp);    /* add on a few special items incl. more armor */
  748. #ifdef MUSE
  749.     m_dowear(mtmp, TRUE);
  750. #endif
  751.  
  752.     if (!in_mklev)
  753.         newsym(mtmp->mx,mtmp->my);    /* make sure the mon shows up */
  754.  
  755.     return(mtmp);
  756. }
  757.  
  758. boolean
  759. enexto(cc, xx, yy, mdat)
  760. coord *cc;
  761. register xchar xx, yy;
  762. struct permonst *mdat;
  763. {
  764.     register xchar x,y;
  765.     coord foo[15], *tfoo;
  766.     int range, i;
  767.     int xmin, xmax, ymin, ymax;
  768.  
  769.     tfoo = foo;
  770.     range = 1;
  771.     do {    /* full kludge action. */
  772.         xmin = max(1, xx-range);
  773.         xmax = min(COLNO-1, xx+range);
  774.         ymin = max(0, yy-range);
  775.         ymax = min(ROWNO-1, yy+range);
  776.  
  777.         for(x = xmin; x <= xmax; x++)
  778.             if(goodpos(x, ymin, (struct monst *)0, mdat)) {
  779.                 tfoo->x = x;
  780. #ifdef MAC_MPW32
  781.                 ( tfoo ) -> y = ymin ;
  782.                 tfoo ++ ;
  783. #else
  784.                 (tfoo++)->y = ymin;
  785. #endif
  786.                 if(tfoo == &foo[15]) goto foofull;
  787.             }
  788.         for(x = xmin; x <= xmax; x++)
  789.             if(goodpos(x, ymax, (struct monst *)0, mdat)) {
  790.                 tfoo->x = x;
  791. #ifdef MAC_MPW32
  792.                 ( tfoo ) -> y = ymax ;
  793.                 tfoo ++ ;
  794. #else
  795.                 (tfoo++)->y = ymax;
  796. #endif
  797.                 if(tfoo == &foo[15]) goto foofull;
  798.             }
  799.         for(y = ymin+1; y < ymax; y++)
  800.             if(goodpos(xmin, y, (struct monst *)0, mdat)) {
  801.                 tfoo->x = xmin;
  802. #ifdef MAC_MPW32
  803.                 ( tfoo ) -> y = y ;
  804.                 tfoo ++ ;
  805. #else
  806.                 (tfoo++)->y = y;
  807. #endif
  808.                 if(tfoo == &foo[15]) goto foofull;
  809.             }
  810.         for(y = ymin+1; y < ymax; y++)
  811.             if(goodpos(xmax, y, (struct monst *)0, mdat)) {
  812.                 tfoo->x = xmax;
  813. #ifdef MAC_MPW32
  814.                 ( tfoo ) -> y = y ;
  815.                 tfoo ++ ;
  816. #else
  817.                 (tfoo++)->y = y;
  818. #endif
  819.                 if(tfoo == &foo[15]) goto foofull;
  820.             }
  821.         range++;
  822.         if(range > ROWNO && range > COLNO) return FALSE;
  823.     } while(tfoo == foo);
  824. foofull:
  825.     i = rn2((int)(tfoo - foo));
  826.     cc->x = foo[i].x;
  827.     cc->y = foo[i].y;
  828.     return TRUE;
  829. }
  830.  
  831. int
  832. goodpos(x, y, mtmp, mdat)
  833. int x,y;
  834. struct monst *mtmp;    /* existing monster being moved, if any */
  835. struct permonst *mdat;
  836. {
  837.     struct monst *mtmp2;
  838.  
  839.     if (x < 1 || x > COLNO-2 || y < 1 || y > ROWNO-2 || MON_AT(x, y))
  840.         return 0;
  841.  
  842.     /* in many cases, we're trying to create a new monster, which
  843.      * can't go on top of the player or any existing monster.
  844.      * however, occasionally we are relocating engravings or objects,
  845.      * which could be colocated and thus get restricted a bit too much.
  846.      * oh well.
  847.      */
  848.     if (x == u.ux && y == u.uy) return 0;
  849.     if ((mtmp2 = m_at(x, y)) && mtmp != mtmp2) return 0;
  850.  
  851.     if (mdat) {
  852.         if (IS_POOL(levl[x][y].typ))
  853.         if (mdat == &playermon &&
  854.             (HLevitation || Wwalking || Magical_breathing))
  855.             return 1;
  856.         else    return (is_flyer(mdat) || is_swimmer(mdat));
  857.         if (levl[x][y].typ == LAVAPOOL)
  858.         if (mdat == &playermon && (HLevitation))
  859.             return 1;
  860.         else return
  861.             (is_flyer(mdat) || (mdat == &mons[PM_FIRE_ELEMENTAL]));
  862.         if (passes_walls(mdat)) return 1;
  863.     }
  864.     if (!ACCESSIBLE(levl[x][y].typ)) return 0;
  865.     if (closed_door(x, y) && (!mdat || !amorphous(mdat)))
  866.         return 0;
  867.     if (sobj_at(BOULDER, x, y) && (!mdat || !throws_rocks(mdat)))
  868.         return 0;
  869.     return 1;
  870. }
  871.  
  872. #endif /* OVL1 */
  873. #ifdef OVLB
  874.  
  875. /*
  876.  * rloc_to()
  877.  *
  878.  * Pulls a monster from its current position and places a monster at
  879.  * a new x and y.  If oldx is 0, then the monster was not in the levels.monsters
  880.  * array.  However, if oldx is 0, oldy may still have a value because mtmp is a
  881.  * migrating_mon.  Worm tails are always placed randomly around the head of
  882.  * the worm.
  883.  */
  884.  
  885. void
  886. rloc_to(mtmp, x, y)
  887.     struct monst *mtmp;
  888.     register int x, y;
  889. {
  890.     register int oldx = mtmp->mx, oldy = mtmp->my;
  891.  
  892.     if(x == mtmp->mx && y == mtmp->my) /* that was easy */
  893.         return;
  894.  
  895.     if (oldx) {                /* "pick up" monster */
  896.         if(mtmp->wormno)
  897.         remove_worm(mtmp);
  898.         else {
  899.         remove_monster(oldx, oldy);
  900.         newsym(oldx, oldy);        /* update old location */
  901.         }
  902.     }
  903.  
  904.     place_monster(mtmp, x, y);        /* put monster down */
  905.  
  906.     if(mtmp->wormno)            /* now put down tail */
  907.         place_worm_tail_randomly(mtmp, x, y);
  908.  
  909.     if(u.ustuck == mtmp){
  910.         if(u.uswallow) {
  911.             u.ux = x;
  912.             u.uy = y;
  913.             docrt();
  914.         } else    u.ustuck = 0;
  915.     }
  916.  
  917.     newsym(x, y);                /* update new location */
  918.     set_apparxy(mtmp);            /* orient monster */
  919. }
  920.  
  921. #endif /* OVLB */
  922. #ifdef OVL2
  923.  
  924. void
  925. rloc(mtmp)
  926.     struct monst *mtmp;
  927. {
  928.     register int x = xupstair, y = yupstair, trycount;
  929.  
  930.     /* if the wiz teleports away to heal, try the up staircase,
  931.        to block the player's escaping before he's healed */
  932.     if (!mtmp->iswiz || !goodpos(x, y, mtmp, mtmp->data)) {
  933.         trycount = 0;
  934.         do {
  935.         x = rn1(COLNO-3,2);
  936.         y = rn2(ROWNO);
  937.         } while(!goodpos(x,y,mtmp,mtmp->data) && ++trycount < 1000);
  938.         /* last ditch attempt to find a good place */
  939.         if (trycount >= 1000) {
  940.         for (x = 2; x < COLNO - 1; x++)
  941.             for (y = 0; y < ROWNO; y++)
  942.             if (goodpos(x,y,mtmp,mtmp->data))
  943.                 goto found_atlast;
  944.         /* level either full of monsters or somehow faulty */
  945.         impossible("rloc(): couldn't relocate monster");
  946.         return;
  947.         }
  948.     }
  949. found_atlast:;
  950.     rloc_to(mtmp, x, y);
  951. }
  952.  
  953. void
  954. rloc_shk(mtmp)   /* to be used when teleporting a shopkeeper */
  955. struct monst *mtmp;
  956. {
  957.     register int x, y, ox, oy, trycount;
  958.  
  959.     if(!mtmp->isshk) return;
  960.     trycount = 0;
  961.     do {
  962.         x = rn1(COLNO-3,2);
  963.         y = rn2(ROWNO);
  964.     } while(!goodpos(x,y,mtmp,mtmp->data) && ++trycount < 1000);
  965.     /* last ditch attempt to find a good place */
  966.     if (trycount >= 1000) {
  967.         for (x = 2; x < COLNO - 1; x++)
  968.             for (y = 0; y < ROWNO; y++)
  969.             if (goodpos(x,y,mtmp,mtmp->data))
  970.                 goto found_ok;
  971.         /* this really shouldn't happen - after all, shopkeeper's
  972.            original position should always be available */
  973.         impossible("rloc_shk(): couldn't relocate shopkeeper");
  974.         return;
  975.     }
  976. found_ok:;
  977.     ox = mtmp->mx;
  978.     oy = mtmp->my;
  979.     rloc_to(mtmp, x, y);
  980.     make_angry_shk(mtmp, ox, oy);
  981. }
  982.  
  983. #endif /* OVL2 */
  984. #ifdef OVLB
  985.  
  986. void
  987. vloc(mtmp)
  988. struct monst *mtmp;
  989. {
  990.     register struct mkroom *croom = search_special(VAULT);
  991.     coord c;
  992.  
  993.     if(croom && somexy(croom, &c) && goodpos(c.x, c.y, mtmp, mtmp->data)) {
  994.         rloc_to(mtmp, c.x, c.y);
  995.         return;
  996.     }
  997.     rloc(mtmp);
  998. }
  999.  
  1000. #endif /* OVLB */
  1001. #ifdef OVL0
  1002.  
  1003. static boolean
  1004. cmavail()    /* return TRUE if "common" monsters can be generated */
  1005. {
  1006.     struct permonst *ptr;
  1007.  
  1008.     for(ptr = &mons[0]; ptr != &mons[NUMMONS]; ptr++)
  1009.        if(!uncommon(ptr))  return TRUE;
  1010.  
  1011.     return FALSE;
  1012. }
  1013.  
  1014. /*
  1015.  *    shift the probability of a monster's generation by
  1016.  *    comparing the dungeon alignment and monster alignment.
  1017.  *    return an integer in the range of 0-5.
  1018.  */
  1019. static int
  1020. align_shift(ptr)
  1021. register struct permonst *ptr;
  1022. {
  1023.     static long NEARDATA oldmoves = 0L;    /* != 1, starting value of moves */
  1024.     static s_level NEARDATA *lev;
  1025.     register int alshift;
  1026.  
  1027.     if(oldmoves != moves) {
  1028.     lev = Is_special(&u.uz);
  1029.     oldmoves = moves;
  1030.     }
  1031.     switch((lev) ? lev->flags.align : dungeons[u.uz.dnum].flags.align) {
  1032.     default:    /* just in case */
  1033.     case AM_NONE:    alshift = 0;
  1034.             break;
  1035.     case AM_LAWFUL:    alshift = (ptr->maligntyp+20)/(2*ALIGNWEIGHT);
  1036.             break;
  1037.     case AM_NEUTRAL:    alshift = (20 - abs(ptr->maligntyp))/ALIGNWEIGHT;
  1038.             break;
  1039.     case AM_CHAOTIC:    alshift = (-(ptr->maligntyp-20))/(2*ALIGNWEIGHT);
  1040.             break;
  1041.     }
  1042.     return alshift;
  1043. }
  1044.  
  1045. struct    permonst *
  1046. rndmonst()        /* select a random monster */
  1047. {
  1048.     register struct permonst *ptr;
  1049.     register int i, ct;
  1050.     register int zlevel;
  1051.     static int NEARDATA minmlev, NEARDATA maxmlev, NEARDATA accept;
  1052.     static long NEARDATA oldmoves = 0L;    /* != 1, starting value of moves */
  1053. #ifdef REINCARNATION
  1054.     static boolean NEARDATA upper;
  1055. #endif
  1056.     static boolean NEARDATA elemlevel;
  1057.  
  1058. #ifdef MULDGN
  1059.     if(u.uz.dnum == quest_dnum && (ptr = qt_montype())) return(ptr);
  1060. #endif
  1061.     if(oldmoves != moves) {        /* must recalculate accept */
  1062.         oldmoves = moves;
  1063.         zlevel = level_difficulty();
  1064.         if(!cmavail()) {
  1065. #ifdef DEBUG
  1066.         pline("cmavail() fails!");
  1067. #endif
  1068.         return((struct permonst *) 0);
  1069.         }
  1070.  
  1071.         /* determine the level of the weakest monster to make. */
  1072.         minmlev = zlevel/6;
  1073.         /* determine the level of the strongest monster to make. */
  1074.         maxmlev = (zlevel + u.ulevel)>>1;
  1075. #ifdef REINCARNATION
  1076.         upper = Is_rogue_level(&u.uz);
  1077. #endif
  1078.         elemlevel = In_endgame(&u.uz) && !Is_astralevel(&u.uz);
  1079. /*
  1080.  *    Find out how many monsters exist in the range we have selected.
  1081.  */
  1082.         accept = 0;
  1083.         for(ct = 0, ptr = &mons[0] ; ptr != &mons[NUMMONS]; ct++, ptr++) {
  1084.         if(tooweak(ct, minmlev) || toostrong(ct, maxmlev))
  1085.             continue;
  1086. #ifdef REINCARNATION
  1087.         if(upper && !isupper(def_monsyms[ptr->mlet])) continue;
  1088. #endif
  1089.         if(elemlevel && wrong_elem_type(ptr)) continue;
  1090.         if(uncommon(ptr)) continue;
  1091.         accept += (ptr->geno & G_FREQ);
  1092.         accept += align_shift(ptr);
  1093.         }
  1094.     }
  1095.  
  1096.     if(!accept) {
  1097. #ifdef DEBUG
  1098.         pline("no accept!");
  1099. #endif
  1100.         return((struct permonst *) 0);
  1101.     }
  1102. /*
  1103.  *    Now, select a monster at random.
  1104.  */
  1105.     ct = rnd(accept);
  1106.     for(i = 0,ptr = &mons[0]; ptr != &mons[NUMMONS] && ct > 0; i++,ptr++) {
  1107.         if(tooweak(i, minmlev) || toostrong(i, maxmlev))
  1108.             continue;
  1109. #ifdef REINCARNATION
  1110.         if(upper & !isupper(def_monsyms[ptr->mlet])) continue;
  1111. #endif
  1112.         if(elemlevel && wrong_elem_type(ptr)) continue;
  1113.         if(uncommon(ptr)) continue;
  1114.         ct -= (ptr->geno & G_FREQ);
  1115.         ct -= align_shift(ptr);
  1116.     }
  1117.     if(ct > 0) {
  1118. #ifdef DEBUG
  1119.         pline("no count!");
  1120. #endif
  1121.         return((struct permonst *) 0);
  1122.     }
  1123.     return(--ptr);    /* subtract extra increment */
  1124. }
  1125.  
  1126. #endif /* OVL0 */
  1127. #ifdef OVL1
  1128.  
  1129. /*    The routine below is used to make one of the multiple types
  1130.  *    of a given monster class.  The second parameter specifies a
  1131.  *    special casing bit mask to allow any of the normal genesis
  1132.  *    masks to be deactivated.  Returns 0 if no monsters
  1133.  *    in that class can be made.
  1134.  */
  1135.  
  1136. struct permonst *
  1137. mkclass(class,spc)
  1138. char    class;
  1139. int    spc;
  1140. {
  1141.     register int    first, last, num = 0;
  1142.     int maxmlev, mask = (G_GENOD | G_EXTINCT | G_NOGEN | G_UNIQ) & ~spc;
  1143.  
  1144.     maxmlev = level_difficulty() >> 1;
  1145.     if(class < 1 || class >= MAXMCLASSES) {
  1146.         impossible("mkclass called with bad class!");
  1147.         return((struct permonst *) 0);
  1148.     }
  1149. /*    Assumption #1:    monsters of a given class are contiguous in the
  1150.  *            mons[] array.
  1151.  */
  1152.     for(first = 0; first < NUMMONS; first++)
  1153.         if (mons[first].mlet == class) break;
  1154.     if (first == NUMMONS) return((struct permonst *) 0);
  1155.  
  1156.     for(last = first; last < NUMMONS && mons[last].mlet == class; last++)
  1157.         if(!(mons[last].geno & mask)) {
  1158.         /* consider it */
  1159.         if(num && toostrong(last, maxmlev) && rn2(2)) break;
  1160.         num += mons[last].geno & G_FREQ;
  1161.         }
  1162.  
  1163.     if(!num) return((struct permonst *) 0);
  1164.  
  1165. /*    Assumption #2:    monsters of a given class are presented in ascending
  1166.  *            order of strength.
  1167.  */
  1168.     for(num = rnd(num); num > 0; first++)
  1169.         if(!(mons[first].geno & mask)) {
  1170.         /* skew towards lower value monsters at lower exp. levels */
  1171.         if(adj_lev(&mons[first]) > (u.ulevel*2)) num--;
  1172.         num -= mons[first].geno & G_FREQ;
  1173.         }
  1174.     first--; /* correct an off-by-one error */
  1175.  
  1176.     return(&mons[first]);
  1177. }
  1178.  
  1179. int
  1180. adj_lev(ptr)    /* adjust strength of monsters based on u.uz and u.ulevel */
  1181. register struct permonst *ptr;
  1182. {
  1183.     int    tmp, tmp2;
  1184.  
  1185.     if((tmp = ptr->mlevel) > 49) return(50); /* "special" demons/devils */
  1186.     tmp2 = (level_difficulty() - tmp);
  1187.     if(tmp2 < 0) tmp--;        /* if mlevel > u.uz decrement tmp */
  1188.     else tmp += (tmp2 / 5);        /* else increment 1 per five diff */
  1189.  
  1190.     tmp2 = (u.ulevel - ptr->mlevel);    /* adjust vs. the player */
  1191.     if(tmp2 > 0) tmp += (tmp2 / 4);        /* level as well */
  1192.  
  1193.     tmp2 = (3 * ((int) ptr->mlevel))/ 2;    /* crude upper limit */
  1194.     return((tmp > tmp2) ? tmp2 : (tmp > 0 ? tmp : 0)); /* 0 lower limit */
  1195. }
  1196.  
  1197. #endif /* OVL1 */
  1198. #ifdef OVLB
  1199.  
  1200. struct permonst *
  1201. grow_up(mtmp,victim)        /* mon mtmp "grows up" to a bigger version. */
  1202. register struct monst *mtmp;
  1203. register struct monst *victim;
  1204. {
  1205.     register int newtype;
  1206.     register struct permonst *ptr = mtmp->data;
  1207.  
  1208.     if (ptr->mlevel >= 50 || is_golem(ptr) || is_home_elemental(ptr)
  1209.         || is_mplayer(ptr))
  1210.         /* doesn't grow up, has strange hp calculation so might be
  1211.          * weakened by tests below */
  1212.         return ptr;
  1213.  
  1214.     if (victim) {
  1215.         mtmp->mhpmax = mtmp->mhpmax + (1 + rn2((int)victim->m_lev+1));
  1216.         if (mtmp->mhpmax <= (8 * (int)mtmp->m_lev)
  1217.             || (mtmp->m_lev == 0 && mtmp->mhpmax <= 4))
  1218.         /* not ready to grow up */
  1219.         return ptr;
  1220.     }
  1221. #ifdef MUSE
  1222.     /* else it's a gain level potion; always go up a level */
  1223.     else {
  1224.         int foo=rnd(8);
  1225.  
  1226.         mtmp->mhp += foo;
  1227.         mtmp->mhpmax += foo;
  1228.     }
  1229. #endif
  1230.  
  1231.     newtype = little_to_big(monsndx(ptr));
  1232.     if ((int) (++mtmp->m_lev) >= mons[newtype].mlevel
  1233.                     && newtype != monsndx(ptr)) {
  1234.         if (mons[newtype].geno & G_GENOD) { /* allow G_EXTINCT */
  1235.             pline("As %s grows up into %s, %s dies!",
  1236.                 mon_nam(mtmp),
  1237.                 an(mons[newtype].mname),
  1238.                 mon_nam(mtmp));
  1239.             mondied(mtmp);
  1240.             return (struct permonst *)0;
  1241.         }
  1242.         mtmp->data = &mons[newtype];
  1243.         mtmp->m_lev = mons[newtype].mlevel;
  1244.     }
  1245.     if (newtype == monsndx(ptr) && victim &&
  1246.         (int) mtmp->m_lev > (3*(int)mtmp->data->mlevel) / 2)
  1247.         mtmp->m_lev = (3*(int)mtmp->data->mlevel) / 2;
  1248.     if (mtmp->m_lev > 0) {
  1249.         if (mtmp->mhp > (int) mtmp->m_lev * 8)
  1250.         mtmp->mhp = mtmp->m_lev * 8;
  1251.         if (mtmp->mhpmax > (int) mtmp->m_lev * 8)
  1252.         mtmp->mhpmax = mtmp->m_lev * 8;
  1253.     }
  1254.     return(mtmp->data);
  1255. }
  1256.  
  1257. #endif /* OVLB */
  1258. #ifdef OVL1
  1259.  
  1260. int
  1261. mongets(mtmp, otyp)
  1262. register struct monst *mtmp;
  1263. register int otyp;
  1264. {
  1265.     register struct obj *otmp;
  1266.  
  1267. #ifdef MUSE
  1268.     if (!otyp) return 0;
  1269. #endif
  1270.     if((otmp = (otyp) ? mksobj(otyp,TRUE,FALSE) : mkobj((char)otyp,FALSE))) {
  1271.         if (mtmp->data->mlet == S_DEMON) {
  1272.         /* demons always get cursed objects */
  1273.         curse(otmp);
  1274.         } else if(is_lminion(mtmp->data)) {
  1275.         /* lawful minions don't get cursed, bad, or rusting objects */
  1276.         otmp->cursed = FALSE;
  1277.         if(otmp->spe < 0) otmp->spe = 0;
  1278.         otmp->oerodeproof = TRUE;
  1279.         } else if(is_mplayer(mtmp->data) && is_sword(otmp))
  1280.             otmp->spe = (3 + rn2(4));
  1281.         if(otmp->otyp == CANDELABRUM_OF_INVOCATION) {
  1282.         otmp->spe = 0;
  1283.         otmp->age = 0L;
  1284.         otmp->lamplit = FALSE;
  1285.         otmp->blessed = otmp->cursed = FALSE;
  1286.         }
  1287.         mpickobj(mtmp, otmp);
  1288.         return(otmp->spe);
  1289.     } else return(0);
  1290. }
  1291.  
  1292. #endif /* OVL1 */
  1293. #ifdef OVLB
  1294.  
  1295. int
  1296. golemhp(type)
  1297. int type;
  1298. {
  1299.     switch(type) {
  1300.         case PM_STRAW_GOLEM: return 20;
  1301.         case PM_ROPE_GOLEM: return 30;
  1302.         case PM_LEATHER_GOLEM: return 40;
  1303.         case PM_WOOD_GOLEM: return 50;
  1304.         case PM_FLESH_GOLEM: return 40;
  1305.         case PM_CLAY_GOLEM: return 50;
  1306.         case PM_STONE_GOLEM: return 60;
  1307.         case PM_IRON_GOLEM: return 80;
  1308.         default: return 0;
  1309.     }
  1310. }
  1311.  
  1312. #endif /* OVLB */
  1313. #ifdef OVL1
  1314.  
  1315. /*
  1316.  *    Alignment vs. yours determines monster's attitude to you.
  1317.  *    ( some "animal" types are co-aligned, but also hungry )
  1318.  */
  1319. boolean
  1320. peace_minded(ptr)
  1321. register struct permonst *ptr;
  1322. {
  1323.     aligntyp mal = ptr->maligntyp, ual = u.ualign.type;
  1324.  
  1325.     if (always_peaceful(ptr)) return TRUE;
  1326.     if (always_hostile(ptr)) return FALSE;
  1327. #ifdef MULDGN
  1328.     if (ptr->msound == MS_LEADER || ptr->msound == MS_GUARDIAN)
  1329.         return TRUE;
  1330.     if (ptr->msound == MS_NEMESIS)    return FALSE;
  1331. #endif
  1332.  
  1333.     /* the monster is hostile if its alignment is different from the
  1334.      * player's */
  1335.     if (sgn(mal) != sgn(ual)) return FALSE;
  1336.  
  1337.     /* Negative monster hostile to player with Amulet. */
  1338.     if (mal < A_NEUTRAL && u.uhave.amulet) return FALSE;
  1339.  
  1340.     /* minions are hostile to players that have strayed at all */
  1341.     if (is_minion(ptr)) return(u.ualign.record >= 0);
  1342.  
  1343.     /* Last case:  a chance of a co-aligned monster being
  1344.      * hostile.  This chance is greater if the player has strayed
  1345.      * (u.ualign.record negative) or the monster is not strongly aligned.
  1346.      */
  1347.     return !!rn2(16 + (u.ualign.record < -15 ? -15 : u.ualign.record)) &&
  1348.         !!rn2(2 + abs(mal));
  1349. }
  1350.  
  1351. /* Set malign to have the proper effect on player alignment if monster is
  1352.  * killed.  Negative numbers mean it's bad to kill this monster; positive
  1353.  * numbers mean it's good.  Since there are more hostile monsters than
  1354.  * peaceful monsters, the penalty for killing a peaceful monster should be
  1355.  * greater than the bonus for killing a hostile monster to maintain balance.
  1356.  * Rules:
  1357.  *   it's bad to kill peaceful monsters, potentially worse to kill always-
  1358.  *    peaceful monsters
  1359.  *   it's never bad to kill a hostile monster, although it may not be good
  1360.  */
  1361. void
  1362. set_malign(mtmp)
  1363. struct monst *mtmp;
  1364. {
  1365.     schar mal = mtmp->data->maligntyp;
  1366.     boolean coaligned;
  1367.  
  1368.     if (mtmp->ispriest || mtmp->isminion) {
  1369.         /* some monsters have individual alignments; check them */
  1370.         if (mtmp->ispriest)
  1371.             mal = EPRI(mtmp)->shralign;
  1372.         else if (mtmp->isminion)
  1373.             mal = EMIN(mtmp)->min_align;
  1374.         /* unless alignment is none, set mal to -5,0,5 */
  1375.         /* (see align.h for valid aligntyp values)     */
  1376.         if(mal != A_NONE)
  1377.             mal *= 5;
  1378.     }
  1379.  
  1380.     coaligned = (sgn(mal) == sgn(u.ualign.type));
  1381. #ifdef MULDGN
  1382.     if (mtmp->data->msound == MS_LEADER) {
  1383.         mtmp->malign = -20;
  1384.     } else
  1385. #endif
  1386.           if (mal == A_NONE) {
  1387.         if (mtmp->mpeaceful)
  1388.             mtmp->malign = 0;
  1389.         else
  1390.             mtmp->malign = 20;    /* really hostile */
  1391.     } else if (always_peaceful(mtmp->data)) {
  1392.         if (mtmp->mpeaceful)
  1393.             mtmp->malign = -3*max(5,abs(mal));
  1394.         else
  1395.             mtmp->malign = 3*max(5,abs(mal)); /* renegade */
  1396.     } else if (always_hostile(mtmp->data)) {
  1397.         if (coaligned)
  1398.             mtmp->malign = 0;
  1399.         else
  1400.             mtmp->malign = max(5,abs(mal));
  1401.     } else if (coaligned) {
  1402.         if (mtmp->mpeaceful)
  1403.             mtmp->malign = -3*max(3,abs(mal));
  1404.         else    /* renegade */
  1405.             mtmp->malign = max(3,abs(mal));
  1406.     } else    /* not coaligned and therefore hostile */
  1407.         mtmp->malign = abs(mal);
  1408. }
  1409.  
  1410. #endif /* OVL1 */
  1411. #ifdef OVLB
  1412.  
  1413. static char NEARDATA syms[] = {
  1414.     MAXOCLASSES, MAXOCLASSES+1, RING_CLASS, WAND_CLASS, WEAPON_CLASS,
  1415.     FOOD_CLASS, GOLD_CLASS, SCROLL_CLASS, POTION_CLASS, ARMOR_CLASS,
  1416.     AMULET_CLASS, TOOL_CLASS, ROCK_CLASS, GEM_CLASS, SPBOOK_CLASS,
  1417.     S_MIMIC_DEF, S_MIMIC_DEF, S_MIMIC_DEF,
  1418. };
  1419.  
  1420. void
  1421. set_mimic_sym(mtmp)        /* KAA, modified by ERS */
  1422. register struct monst *mtmp;
  1423. {
  1424.     int typ, roomno, rt;
  1425.     unsigned appear, ap_type;
  1426.     int s_sym;
  1427.     struct obj *otmp;
  1428.     int mx, my;
  1429.  
  1430.     if (!mtmp) return;
  1431.     mx = mtmp->mx; my = mtmp->my;
  1432.     typ = levl[mx][my].typ;
  1433.                     /* only valid for INSIDE of room */
  1434.     roomno = levl[mx][my].roomno - ROOMOFFSET;
  1435.     if (roomno >= 0)
  1436.         rt = rooms[roomno].rtype;
  1437. #ifdef SPECIALIZATION
  1438.     else if (IS_ROOM(typ))
  1439.         rt = OROOM,  roomno = 0;
  1440. #endif
  1441.     else    rt = 0;    /* roomno < 0 case for GCC_WARN */
  1442.  
  1443.     if (OBJ_AT(mx, my)) {
  1444.         ap_type = M_AP_OBJECT;
  1445.         appear = level.objects[mx][my]->otyp;
  1446.     } else if (IS_DOOR(typ) || IS_WALL(typ) ||
  1447.            typ == SDOOR || typ == SCORR) {
  1448.         ap_type = M_AP_FURNITURE;
  1449.         /*
  1450.          *  If there is a wall to the left that connects to this
  1451.          *  location, then the mimic mimics a horizontal closed door.
  1452.          *  This does not allow doors to be in corners of rooms.
  1453.          */
  1454.         if (mx != 0 &&
  1455.             (levl[mx-1][my].typ == HWALL    ||
  1456.              levl[mx-1][my].typ == TLCORNER ||
  1457.              levl[mx-1][my].typ == TRWALL   ||
  1458.              levl[mx-1][my].typ == BLCORNER ||
  1459.              levl[mx-1][my].typ == TDWALL   ||
  1460.              levl[mx-1][my].typ == CROSSWALL||
  1461.              levl[mx-1][my].typ == TUWALL    ))
  1462.             appear = S_hcdoor;
  1463.         else
  1464.             appear = S_vcdoor;
  1465.  
  1466.         if(!mtmp->minvis || See_invisible)
  1467.             block_point(mx,my);    /* vision */
  1468.     } else if (level.flags.is_maze_lev && rn2(2)) {
  1469.         ap_type = M_AP_OBJECT;
  1470.         appear = STATUE;
  1471.     } else if (roomno < 0) {
  1472.         ap_type = M_AP_OBJECT;
  1473.         appear = BOULDER;
  1474.         if(!mtmp->minvis || See_invisible)
  1475.             block_point(mx,my);    /* vision */
  1476.     } else if (rt == ZOO || rt == VAULT) {
  1477.         ap_type = M_AP_OBJECT;
  1478.         appear = GOLD_PIECE;
  1479.     } else if (rt == DELPHI) {
  1480.         if (rn2(2)) {
  1481.             ap_type = M_AP_OBJECT;
  1482.             appear = STATUE;
  1483.         } else {
  1484.             ap_type = M_AP_FURNITURE;
  1485.             appear = S_fountain;
  1486.         }
  1487.     } else if (rt == TEMPLE) {
  1488.         ap_type = M_AP_FURNITURE;
  1489.         appear = S_altar;
  1490.     /*
  1491.      * We won't bother with beehives, morgues, barracks, throne rooms
  1492.      * since they shouldn't contain too many mimics anyway...
  1493.      */
  1494.     } else if (rt >= SHOPBASE) {
  1495.         s_sym = get_shop_item(rt - SHOPBASE);
  1496.         if (s_sym < 0) {
  1497.             ap_type = M_AP_OBJECT;
  1498.             appear = -s_sym;
  1499.         } else {
  1500.             if (s_sym == RANDOM_CLASS)
  1501.                 s_sym = syms[rn2((int)sizeof(syms)-2) + 2];
  1502.             goto assign_sym;
  1503.         }
  1504.     } else {
  1505.         s_sym = syms[rn2((int)sizeof(syms))];
  1506. assign_sym:
  1507.         if (s_sym >= MAXOCLASSES) {
  1508.             ap_type = M_AP_FURNITURE;
  1509.             appear = s_sym == MAXOCLASSES ? S_upstair : S_dnstair;
  1510.         } else if (s_sym == GOLD_CLASS) {
  1511.             ap_type = M_AP_OBJECT;
  1512.             appear = GOLD_PIECE;
  1513.         } else {
  1514.             ap_type = M_AP_OBJECT;
  1515.             if (s_sym == S_MIMIC_DEF) {
  1516.                 appear = STRANGE_OBJECT;
  1517.             } else {
  1518.                 otmp = mkobj( (char) s_sym, FALSE );
  1519.                 appear = otmp->otyp;
  1520.                 /* make sure container contents are free'ed */
  1521.                 obfree(otmp, (struct obj *) 0);
  1522.             }
  1523.         }
  1524.     }
  1525.     mtmp->m_ap_type = ap_type;
  1526.     mtmp->mappearance = appear;
  1527. }
  1528.  
  1529. #endif /* OVLB */
  1530.  
  1531. /*makemon.c*/
  1532.